package com.vitaf.cash.app.base

import android.app.AlertDialog
import android.content.Context
import android.os.Bundle
import android.view.View
import com.vitaf.cash.app.R
import com.vitaf.cash.app.ui.dialog.VitafLoadingDlog
import com.vitaf.cash.app.utils.manager.VitafToastManager
import com.vitaf.cash.lib.adapter.VitafOnSingleClickListener

abstract class VitafBaseDlog(
    context: Context,
    style: Int = R.style.DialogTheme
) : AlertDialog(context, style), IVitafBaseView, View.OnClickListener {

    // 获取页面名称，子类必须实现
    protected abstract val pageName: String
    //备注信息
    protected open var pageRemark: String = ""
    // 是否启用页面埋点（默认启用）
    protected open val enableDialogTrack: Boolean = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(createVitafContentView())
        initializeVitafWindow()
        initializeVitafViews()
        setupVitafEventListeners()
        loadVitafData()
    }

    protected abstract fun createVitafContentView(): View

    /**
     * 初始化Window
     */
    protected abstract fun initializeVitafWindow()

    /**
     * 初始化布局
     */
    protected abstract fun initializeVitafViews()

    /**
     * 初始化监听
     */
    protected abstract fun setupVitafEventListeners()

    /**
     * 初始化数据
     */
    protected abstract fun loadVitafData()

    /**
     * 控件点击事件
     */
    protected abstract fun onVitafEventViewClick(v: View?)

    override fun showVitafLoading() {
        VitafLoadingDlog.show(context, false, false, "")
    }

    override fun hideVitafLoading() {
        VitafLoadingDlog.dismiss(context)
    }

    override fun showVitafToast(s: String?) {
        VitafToastManager.showShortText(context, s)
    }

    override fun onClick(v: View) {
        onVitafEventViewClick(v)
    }

    protected fun setOnClickListener(view: View) {
        view.setOnClickListener(object : VitafOnSingleClickListener() {
            override fun onSingleClick(v: View) {
                onVitafEventViewClick(v)
            }
        })
    }

    protected fun setOnClickListener(vararg views: View) {
        views.forEach { view ->
            view.setOnClickListener(object : VitafOnSingleClickListener() {
                override fun onSingleClick(v: View) {
                    onVitafEventViewClick(v)
                }
            })
        }
    }

    override fun dismiss() {
        super.dismiss()
        VitafLoadingDlog.dismiss(context)
    }

}
